// 移除全局console重写，避免干扰构建过程

import { defineConfig, type UserConfigExport } from '@tarojs/cli'
// import { UnifiedWebpackPluginV5 } from 'weapp-tailwindcss/webpack'
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'
import devConfig from './dev'
import prodConfig from './prod'
import path from 'path'

// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
export default defineConfig<'webpack5'>((merge, { }) => {
    const baseConfig: UserConfigExport<'webpack5'> = {
        cacheDirectory: path.resolve(__dirname, 'node_modules/.cache/taro'),
        alias: {
            '@': path.resolve(__dirname, 'src'),
        },
        projectName: 'h5',
        date: '2025-11-7',
        designWidth: 750,
        deviceRatio: {
            640: 2.34 / 2,
            750: 1,
            375: 2,
            828: 1.81 / 2
        },
        sourceRoot: 'src',
        outputRoot: 'dist',
        plugins: [
            "@tarojs/plugin-generator"
        ],
        defineConstants: {
        },
        copy: {
            patterns: [
            ],
            options: {
            }
        },
        framework: 'react',
        compiler: 'webpack5',
        cache: {
            enable: true // 开启Webpack持久化缓存，提升二次编译速度
        },
        mini: {
            postcss: {
                pxtransform: {
                    enable: true,
                    config: {

                    }
                },
                cssModules: {
                    enable: true, // 启用 css modules 功能
                    config: {
                        namingPattern: 'module', // 转换模式，取值为 global/module
                        generateScopedName: '[name]__[local]___[hash:base64:5]'
                    }
                }
            },
            webpackChain(chain) {
                chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
            }
        },
        h5: {
            publicPath: '/',
            staticDirectory: 'static',
            output: {
                filename: 'js/[name].[hash:8].js',
                chunkFilename: 'js/[name].[chunkhash:8].js'
            },
            miniCssExtractPluginOption: {
                ignoreOrder: true,
                filename: 'css/[name].[hash].css',
                chunkFilename: 'css/[name].[chunkhash].css'
            },
            postcss: {
                autoprefixer: {
                    enable: true,
                    config: {}
                },
                cssModules: {
                    enable: true, // 启用 css modules 功能
                    config: {
                        namingPattern: 'module', // 转换模式，取值为 global/module
                        generateScopedName: '[name]__[local]___[hash:base64:5]'
                    }
                }
            },
            webpackChain(chain) {
                chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)



                // 简化的代码分割配置
                chain.optimization.splitChunks({
                    chunks: 'all',
                    cacheGroups: {
                        vendors: {
                            name: 'vendors',
                            test: /[\\/]node_modules[\\/]/,
                            priority: -10
                        }
                    }
                })

                // 禁用性能警告
                chain.performance.hints(false)

                // 配置缓存策略，使用webpack-chain支持的API
                chain.cache({
                    type: 'filesystem',
                    buildDependencies: {
                        config: [__filename]
                    }
                })

                // 移除所有可能产生日志的插件
                const pluginNames = ['webpackbar', 'friendly-errors-webpack-plugin', 'progress-bar-webpack-plugin']
                pluginNames.forEach(name => {
                  if (chain.plugins.has(name)) {
                    chain.plugins.delete(name)
                  }
                })
                
                // 最严格的stats配置，仅输出错误和警告
                chain.stats({
                  all: false,
                  errors: true,
                  warnings: false,
                  errorDetails: true,
                  timings: false,
                  entrypoints: false,
                  modules: false,
                  reasons: false,
                  version: false
                })
                
                // 禁用性能警告
                chain.performance.hints(false)
                
                // 启用热更新，这是React Fast Refresh工作的必要条件
                chain.devServer.hot(true)
                
                // 已在配置文件顶部全局重写console方法，简化日志输出
            }
        },
        rn: {
            appName: 'taroDemo',
            postcss: {
                cssModules: {
                    enable: true, // 启用 css modules 功能
                }
            }
        }
    }


    if (process.env.NODE_ENV === 'development') {
        // 本地开发构建配置（不混淆压缩）
        return merge({}, baseConfig, devConfig)
    }
    // 生产构建配置（默认开启压缩混淆等）
    return merge({}, baseConfig, prodConfig)
})
